package com.ilearn.spring.future.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;

/**
 * @Project: cmcchq
 * @Description:
 * @Version 1.0
 * @Throws :
 * @Author: <li>2019/6/26 lyndon Create 1.0
 * @Copyright (c)2019-2019 版权所有
 * @Modified By:
 */

@Slf4j
@Component
public class AsyncExceptionDemo {

    @Async
    public void simple() {
        System.out.println("simple is ran in " + Thread.currentThread().getName());
        log.info("this is a void method");
    }

    @Async
    public void inputDemo(String s) {
        System.out.println("inputDemo is ran in " + Thread.currentThread().getName());
        log.info("this is a input method, {}", s);
        throw new IllegalArgumentException("inputError");
    }

    @Async
    public Future hardDemo(String s) {
        System.out.println("hardDemo is ran in " + Thread.currentThread().getName());
        log.info("this is a hard method, {}", s);
        Future future;
        try {
            Thread.sleep(2000);
            throw new IllegalArgumentException();
        } catch (InterruptedException e) {
            future = new AsyncResult("InterrupterException error");
        } catch (IllegalArgumentException e) {
            future = new AsyncResult("IllegalArgumentException error");
        }
        return future;
    }

}
